package util.translate;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TranslatorRobotPost {

	public void translate() throws ClassNotFoundException, SQLException, IOException {
		Connection conn = null;
		Connection connInsert = null;
		Statement stmt = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		boolean alltime = true;
		Translator translator = new Translator();
		GoogleTranslate gTranslator = new GoogleTranslate();
		Class.forName("com.mysql.jdbc.Driver");
		while (alltime) {

			conn = DriverManager.getConnection("jdbc:mysql://" + dbhost + "/" + dbname, "root", "777888999a!");
			connInsert = DriverManager.getConnection("jdbc:mysql://" + hostDG + "/" + dbnameDG, "root", "777888999a!");
			stmt = conn.createStatement();
			int fullTimes = 0;
			rs = stmt.executeQuery("select id,newstext from phome_ecms_news_data_1 where id > " + workingid + " limit 100");
			while (rs.next()) {
				pstmt = connInsert.prepareStatement("insert into phome_ecms_news_data_1 (id,newstext) values (?,?)");
				pstmt.setInt(1, rs.getInt("id"));
				String content = new String();
				if (rs.getString("newstext").length() > 30000) {
					content = gTranslator.getTString(rs.getString("newstext"));
				} else {
					content = translator.getTString(rs.getString("newstext"));
				}
				pstmt.setString(2, content);
				pstmt.execute();
				System.out.println(dbname + " --- " + rs.getInt("id"));
				fullTimes++;
				workingid = rs.getInt(1);
			}
			if (fullTimes < 100) {
				break;
			} else {
				continue;
			}
		}
	}

	String dbhost = new String();

	public void setDbhost(String dbhost) {
		this.dbhost = dbhost;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

	public void setHostDG(String hostDG) {
		this.hostDG = hostDG;
	}

	public void setDbnameDG(String dbnameDG) {
		this.dbnameDG = dbnameDG;
	}

	String dbname = new String();
	String hostDG = new String();
	String dbnameDG = new String();
	int workingid = 0;

	public void setWorkingid(int workingid) {
		this.workingid = workingid;
	}

	public int getWorkingid() {
		return workingid;
	}

}
